<?php

namespace Modules\Admin\Http\Controllers;

use HiCommon\Service\PermissionsService;
use Illuminate\Support\Facades\Request;

class PermissionController extends BaseController
{
    /**
     * 权限列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        /*清除权限缓存*/
        clear_cache_for_all();
        $arr_model = [
          'admin'=>'管理后台',
          'shop_check'=>'商户后台-检测机构',
          'shop_repair'=>'商户后台-维修店',
          'shop_roadhelp'=>'商户后台-道路救援',
          'shop_check_app'=>'商户APP-检测机构',
          'shop_repair_app'=>'商户APP-维修店',
//          'shop_roadhelp_app'=>'商户APP-道路救援',
        ];
        $s_model = Request::get('model','admin');
        $arr_permission = collect(ztree_permissions())->where('model',$s_model)->values()->all();

        $arr_data = [
          'arr_permission'=>json_encode($arr_permission),
          'arr_model'=>$arr_model,
          's_model'=>$s_model
        ];
        return view('admin::permission.index',$arr_data);
    }

    /**
     * 拖拽移动权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function drag_move_node(){
        try {
            $arr_input = Request::post();
            return  PermissionsService::api_drag_move_node($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

//    /**
//     * 添加权限页面
//     *
//     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function create()
//    {
//        $n_id = (int)Request::get('id',0);
//        if($n_id > 0){
//            $arr_data = ['n_id'=>$n_id,'title'=>'添加子权限'];
//        }
//        else{
//            $arr_data = ['n_id'=>$n_id,'title'=>'添加顶级权限'];
//        }
//        return view('admin::permission.create', $arr_data);
//    }

    /**
     * 添加权限操作
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function store()
    {
        try {
            $arr_input = Request::input();
            //        $arr_input['operator_id'] = (int)admin_user_id();
            return PermissionsService::api_add_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

//    /**
//     * 修改权限页面
//     *
//     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function edit()
//    {
//        $nId = Request::get('id',0);
//        $res = PermissionService::get_one((int)$nId);
//        echo_error($res);
//
//        $arr_data = [
//          'arr_data'=>$res['data'],
//          'select_categorys'=>MenuService::get_permission_tree(),
//        ];
//        return view('admin::permission.edit',$arr_data);
//    }

    /**
     * 修改管理员页面操作
     *
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function update()
    {
        try {
            $arr_input = Request::input();
            //        $arr_input['operator_id'] = (int)admin_user_id();
            return PermissionsService::api_edit_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }


//    /**
//     * 更改权限状态
//     *
//     * @return array|\Illuminate\Http\RedirectResponse
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function change_state()
//    {
//        $arr_input = Request::input();
//        return PermissionsService::change_one_state($arr_input);
//    }

    /**
     * 删除权限
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        try {
            $n_id = (int)Request::get('id',0);
            return PermissionsService::api_delete_one($n_id);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }


}
